//
// Created by 30938 on 2022/8/29.
//

#include "BigFileOperate.h"
void BigFileOperate::fileSplit(const string &filePath, const string &destDir, streamsize eachSize){
    ifstream ifs(filePath,ios::binary);
    string filename = destDir+"/"+filePath.substr(filePath.find_last_of("\\/")+1);
    streamsize eachByteSize = eachSize*1024*1024;
    int index=1;
    streamsize currReadSize=0;
    ofstream ofs(filename+".1",ios::binary);
    while(!ifs.eof()){
        char buffer[4096]={0};
        ifs.read(buffer,sizeof(buffer));
        streamsize cnt=ifs.gcount();  // 本次实际读了多少个字节
        currReadSize +=cnt;
        if(currReadSize/eachSize>=index){
            ofs.close();
            ofs.open(filename+"."+ to_string(++index),ios::binary);
        }
        ofs.write(buffer,cnt);
        ofs.flush();
    }
    ofs.close();
    ifs.close();
}